package com.ccpdown.util.socket;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;

public class NetWorkUtil {

	public static boolean chkNetWork(final Context context) {
		boolean flag = false;
		try {
			ConnectivityManager cwjManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (cwjManager.getActiveNetworkInfo() != null)
				flag = cwjManager.getActiveNetworkInfo().isAvailable();
			if (!flag) {
				String str = "请开启GPRS或WIFI网络连接";
				Builder b = new AlertDialog.Builder(context)
						.setTitle("没有可用的网络").setMessage(str);
				b.setNegativeButton("确定",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								Intent mIntent = new Intent("/");
								ComponentName comp = new ComponentName(
										"com.android.settings",
										"com.android.settings.WirelessSettings");
								mIntent.setComponent(comp);
								mIntent.setAction("android.intent.action.VIEW");
								context.startActivity(mIntent);
							}
						})
						.setNeutralButton("取消",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										dialog.cancel();
									}
								}).create();
				b.show();
			}
		} catch (Exception e) {
			e.printStackTrace();
			flag=false;
		}
		return flag;
	}
}
